<template>
  <el-card>
    <div slot="header" class="df aic jcsb">
      <span>店铺管理</span>
      <el-button
        v-if="!isEdit"
        size="small"
        @click="isEdit = !isEdit"
        type="primary"
        >编辑</el-button
      >
      <el-button v-else @click="isEdit = !isEdit;submit()"  size="small"
        type="success">完成</el-button>
    </div>
    <!-- 
    name	店铺名称
    bulletin	公告
    avatar	店铺头像
    deliveryPrice	配送费
    deliveryTime	送达时间
    description	描述
    score	评分
    sellCount	销量
    supports	活动 数组 []
    pics	店铺图片 数组 []
    date	营业时间 数组 []
    minPrice	起送价格  -->
    <!-- 表单 -->
    <el-form label-width="80px" class="form" :model="formData">
      <el-form-item label="店铺名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="店铺名称">
        <el-input
          type="textarea"
          :rows="5"
          v-model="formData.bulletin"
        ></el-input>
      </el-form-item>
      <el-form-item label="店铺头像">
        <el-upload
          class="avatar-uploader"
          :action="`${API}/shop/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="formData.avatar" :src="formData.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="店铺图片">
        <el-upload
          :action="`${API}/shop/upload`"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :file-list="formData.pics"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="配送费">
        <el-input-number
          v-model="formData.deliveryPrice"
          :min="1"
          :max="1000"
          label="配送费"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="配送时间">
        <el-input-number
          v-model="formData.deliveryTime"
          :min="1"
          :max="1000"
          label="配送时间"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="配送描述">
        <el-input v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="店铺评分" class="df aic rate">
        <el-rate
          disabled
          score-template="{value}"
          v-model="formData.score"
          show-score
        ></el-rate>
      </el-form-item>
      <el-form-item label="销量">
        <el-input-number
          v-model="formData.sellCount"
          :min="1"
          :max="100000"
          label="销量"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="活动">
        <el-checkbox-group v-if="formData.supports" v-model="formData.supports">
          <el-checkbox label="在线支付满28减5"></el-checkbox>
          <el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
          <el-checkbox label="单人精彩套餐"></el-checkbox>
          <el-checkbox label="特价饮品8折抢购"></el-checkbox>
          <el-checkbox label="单人特色套餐"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="营业时间">
        <el-time-picker
          is-range
          v-model="formData.date"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="选择时间范围"
        ></el-time-picker>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import { postEditShop, getShopInfo } from "@/api/shop";
export default {
  data() {
    return {
      //服务器地址
      API: "http://sell.h5.itsource.cn:8087",
      dialogImageUrl: "",
      dialogVisible: false,
      isEdit: false,
      formData: {},
    };
  },
  methods: {
    //约束上传文件类型 & 上传文件大小
    beforeAvatarUpload(file) {
      const isJPG = /(png|jpeg)/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是PNG JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    
    async getlinkShop() {
      let res = await getShopInfo();
      console.log(res);
      let pics = res.data.data.pics.map((v) => {
        return {
          url: v,
        };
      });
      //修改评分
      let score = res.data.data.score / 20;
      this.formData = { ...res.data.data,pics, score };
    },
    //上传成功触发的函数
    async handleAvatarSuccess(res) {
      let { code, msg, imgUrl } = res;
      if (code === 0) {
        //发请求修改头像
        this.$message.success(msg);
        this.formData.avatar = imgUrl;
      }
    },
    //多图上传成功的钩子函数
    handleShopUpload(res){
       //解构数据
      let { code, msg, imgUrl } = res;
      //判断
      if (code === 0) {
        //修改数据
        this.formData.pics.push({
          url: imgUrl,
        });
        this.$message.success(msg);
      }
    },
     handleRemove(file, fileList) {
      //直接数据赋值
      this.formData.pics = fileList;
    },
     handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //修改店铺内容
    async submit(){
      //处理数据
    let pics = this.formData.pics.map((v)=>v.url)
    
    let score = this.formData.score*20;
    let res = await postEditShop({
      ...this.formData,
      pics,
      score
    })
    }
  },
  created() {
    this.getlinkShop();
  },
};
</script>

<style lang="scss" scoped>
.shop {
  padding: 20px;
  .el-input {
    width: 400px;
  }
  .avatar-uploader {
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    .el-icon-plus {
      font-size: 20px;
    }
  }
}
</style>